from pyjenkinsci.jenkinsobject import jenkinsobject
from pyjenkinsci.artifact import artifact
from pyjenkinsci.result_set import result_set
import time
import logging
from pyjenkinsci import config
from pyjenkinsci.exceptions import NoResults, FailedNoResults
from pyjenkinsci.constants import STATUS_FAIL, STATUS_ABORTED, RESULTSTATUS_FAILURE

log = logging.getLogger(__name__)

class build( jenkinsobject ):
    """
    Represents a hudson environment.
    """
    
    STR_TOTALCOUNT = "totalCount"
    STR_TPL_NOTESTS_ERR = "%s has status %s, and does not have any test results"
    
    def __init__( self, url, buildno, job ):
        assert type(buildno) == int
        self.buildno = buildno
        self.job = job
        jenkinsobject.__init__( self, url )
        
    def __str__(self):
        return self._data[ 'fullDisplayName' ]
    
    def id(self):
        return self._data["number"]
    
    def getStatus(self):
        return self._data["result"]
    
    def getDuration(self):
        return self._data["duration"]
    
    def getArtifacts( self ):
        for afinfo in self._data["artifacts"]:
            url = "%sartifact/%s" % ( self.baseurl, afinfo["relativePath"] )            
            af = artifact( afinfo["fileName"], url, self )
            yield af
            del af, url
            
    def getArtifactDict(self):
        return dict( (a.filename, a) for a in self.getArtifacts() )
    
    def is_running( self ):
        """
        Return a bool if running.
        """
        self.poll()
        return self._data["building"]
    
    def is_good( self ):
        """
        Return a bool, true if the build was good.
        If the build is still running, return False.
        """
        return ( not self.is_running() ) and self._data["result"] == 'SUCCESS'
    
    def block_until_complete(self, delay=15):
        assert isinstance( delay, int )
        count = 0
        while self.is_running():
            total_wait = delay * count
            log.info("Waited %is for %s #%s to complete" % ( total_wait, self.job.id(), self.id() ) )
            time.sleep( delay )
            count += 1
    
    def getHudsonObject(self):
        return self.job.getHudsonObject()
    
    def getResultUrl(self):
        """
        Return the URL for the object which provides the job's result summary.
        """
        url_tpl = r"%stestReport/%s"
        return  url_tpl % ( self._data["url"] , config.JENKINS_API ) 
    
    def getResultSet(self):
        """    
        Obtain detailed results for this build.
        """
        result_url = self.getResultUrl()
        
        if not self.getActions().has_key( "totalCount" ):
            raise NoResults( "%s does not have any published results" % str(self) )
        
        buildstatus = self.getStatus()
        if buildstatus in [ STATUS_FAIL, RESULTSTATUS_FAILURE, STATUS_ABORTED ]:
            raise FailedNoResults( self.STR_TPL_NOTESTS_ERR % ( str(self), buildstatus ) ) 
        
        if self.getActions()[ self.STR_TOTALCOUNT ] == 0:
            raise NoResults( self.STR_TPL_NOTESTS_ERR % ( str(self), buildstatus ) ) 
        
        obj_results = result_set( result_url, build=self )
        

        
        return obj_results
        
    def hasResultSet(self):
        """
        Return a boolean, true if a result set is available. false if not.
        """
        
    def getActions(self):
        all_actions = {}
        for dct_action in self._data["actions"]:
            all_actions.update( dct_action )
        return all_actions
        